package com.zcx.qshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.adapter.ThreeListAdapter;
import com.zcx.qshop.conn.JsonGoodslistAsyGet;
import com.zcx.qshop.conn.JsonTypelistAsyGet;
import com.zcx.qshop.dialog.ScreenDialog;
import com.zcx.qshop.entity.Good;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.qshop.view.ThreeBarView;
import com.zcx.qshop.view.TitleView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2015/8/23 0023.
 */
public class ThreeListActivity extends QSActivity {

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.three_list_three_bar_biew) private ThreeBarView threeBarView;

    @BoundView(R.id.three_list_pull_to_refresh_list_view) private PullToRefreshListView pullToRefreshListView;

    private List<JsonGoodslistAsyGet.Info.GoodDouble> list = new ArrayList<>();

    private ThreeListAdapter adapter;

    private JsonTypelistAsyGet.TypeOne typeOne, typeTwo;

    private JsonGoodslistAsyGet.Info info;

    private JsonGoodslistAsyGet jsonGoodslistAsyGet = new JsonGoodslistAsyGet(QSApplication.QSPreferences.readCity().code, "", "", "1", ScreenDialog.Screen.THREE.serchtype, "", new AsyCallBack<JsonGoodslistAsyGet.Info>(){

        private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

        @Override
        public void onSuccess(String toast, int type, JsonGoodslistAsyGet.Info info) throws Exception {

            if(type == 0){

                list.clear();

                list.addAll((ThreeListActivity.this.info = info).goodDoubles);

                adapter.notifyDataSetInvalidated();

            }else{

                list.addAll((ThreeListActivity.this.info = info).goodDoubles);

                adapter.notifyDataSetChanged();

            }

        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshListView.setLastUpdatedLabel(simpleDateFormat.format(new Date(System.currentTimeMillis())));

            pullToRefreshListView.onPullUpRefreshComplete();

            pullToRefreshListView.onPullDownRefreshComplete();

        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_three_list);

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.NAME_ONE, TitleView.Type.SCREEN);

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

            @Override
            public void onScreenClick() {

                new ScreenDialog(ThreeListActivity.this) {

                    @Override
                    protected void onItemClick(Screen itemScreen) {

                        jsonGoodslistAsyGet.serchtype = itemScreen.serchtype;

                        jsonGoodslistAsyGet.page = "1";

                        jsonGoodslistAsyGet.execute(ThreeListActivity.this, true, 0);

                    }

                }.show();

            }

        });

        pullToRefreshListView.setPullRefreshEnabled(true);

        pullToRefreshListView.setScrollLoadEnabled(true);

        pullToRefreshListView.setPullLoadEnabled(false);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                jsonGoodslistAsyGet.page = "1";

                jsonGoodslistAsyGet.execute(ThreeListActivity.this, false, 0);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if (info != null && info.ispage.equals("1")) {

                    jsonGoodslistAsyGet.page = info.nextpage;

                    jsonGoodslistAsyGet.execute(ThreeListActivity.this, false, 1);

                } else {

                    UtilToast.show(ThreeListActivity.this, "没有更多数据");

                    pullToRefreshListView.onPullUpRefreshComplete();

                    pullToRefreshListView.onPullDownRefreshComplete();

                }

            }

        });

        ListView listView = pullToRefreshListView.getRefreshableView();

        listView.setDivider(null);

        listView.setVerticalScrollBarEnabled(false);

        listView.setAdapter(adapter = new ThreeListAdapter(this, list) {

            @Override
            protected void onDetail(Good good) {

                startVerifyActivity(CommodityDetailActivity.class, new Intent().putExtra("Good", good));

            }

        });

//        typeOne = (JsonTypelistAsyGet.TypeOne) getIntent().getSerializableExtra("TypeOne");

        typeTwo = (JsonTypelistAsyGet.TypeOne) getIntent().getSerializableExtra("TypeTwo");

        if(typeOne != null){

            titleView.setTitleName(typeOne.typetitle);

            threeBarView.setVisibility(View.GONE);

            jsonGoodslistAsyGet.ftypeid = typeOne.typeid;

        }

        if(typeTwo != null){

            titleView.setTitleName(typeTwo.typetitle);

            jsonGoodslistAsyGet.stypeid = typeTwo.typeid;

            threeBarView.setOnItemClickListener(new ThreeBarView.OnItemClickListener() {

                @Override
                public void onItemClick(JsonTypelistAsyGet.TypeTwo typeTwo) {

                    jsonGoodslistAsyGet.ttypeid = typeTwo.nextid;

                    jsonGoodslistAsyGet.execute(ThreeListActivity.this);

                }

            });

            threeBarView.setTypeThrees(typeTwo.typeTwos);

        }else{

            jsonGoodslistAsyGet.execute(this);

        }

    }

}